package waal.pc.frontend.tiles;

import java.awt.BasicStroke;
import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Font;
import java.awt.GradientPaint;
import java.awt.Point;
import java.text.DecimalFormat;

import org.jfree.chart.ChartPanel;
import org.jfree.chart.JFreeChart;
import org.jfree.chart.plot.dial.ArcDialFrame;
import org.jfree.chart.plot.dial.DialBackground;
import org.jfree.chart.plot.dial.DialPlot;
import org.jfree.chart.plot.dial.DialPointer;
import org.jfree.chart.plot.dial.DialTextAnnotation;
import org.jfree.chart.plot.dial.DialValueIndicator;
import org.jfree.chart.plot.dial.StandardDialScale;
import org.jfree.data.general.DefaultValueDataset;
import org.jfree.ui.GradientPaintTransformType;
import org.jfree.ui.StandardGradientPaintTransformer;

import waal.pc.backend.channels.Channel;

public class MeterTile extends ChannelTile implements UpdateableTile {


	private static final long serialVersionUID = 5714148626471580285L;
	private DefaultValueDataset dataset; 
	private Channel channel;
		
	public MeterTile() {
		super("Meter");
		this.setLayout(new BorderLayout());
	}
	
	@Override
	public ChannelTile getTile(Channel channel) {
		this.initTile(channel);
		this.channel = channel;
		this.dataset = new DefaultValueDataset();
		double max = channel.getMax();
		double multi = 1;
		while(max > 10) {
			max /= 10;
			multi *= 10;
		}
		double pmax = Math.ceil(max) * multi;
		double scale =1*multi;
		double pmin = channel.getMin(); 
		
		DialPlot plot = new DialPlot(this.dataset);
		ArcDialFrame af = new ArcDialFrame(20D, 140D);
		af.setInnerRadius(0.2);
		af.setOuterRadius(0.95);
		af.setForegroundPaint(Color.DARK_GRAY);
		af.setStroke(new BasicStroke(1F));
		plot.setDialFrame(af);
		GradientPaint gradientpaint = new GradientPaint(new Point(), new Color(255, 255, 255), new Point(), new Color(240, 240, 240));
		DialBackground dialbackground = new DialBackground(gradientpaint);
		dialbackground.setGradientPaintTransformer(new StandardGradientPaintTransformer(GradientPaintTransformType.VERTICAL));
		plot.addLayer(dialbackground);
		StandardDialScale standarddialscale = new StandardDialScale(pmin, pmax, 150, -120D, scale, 4);
		standarddialscale.setTickRadius(0.9D);
		standarddialscale.setTickLabelOffset(0.15D);
		plot.addScale(0, standarddialscale);
		DialTextAnnotation dialtextannotation = new DialTextAnnotation(this.channel.getUnit());
		dialtextannotation.setRadius(0.52);
		dialtextannotation.setAngle(50);
		dialtextannotation.setPaint(Color.BLACK);
		plot.addLayer(dialtextannotation);
		DialValueIndicator dialvalueindicator = new DialValueIndicator(0);
		dialvalueindicator.setFont(new Font("Dialog", 0, 10));
		dialvalueindicator.setOutlinePaint(Color.darkGray);
		dialvalueindicator.setNumberFormat(new DecimalFormat ( ",##0.0000" ));
		dialvalueindicator.setRadius(0.35);
		dialvalueindicator.setAngle(100);
		plot.addLayer(dialvalueindicator);
		DialPointer.Pin pin = new org.jfree.chart.plot.dial.DialPointer.Pin();
		pin.setRadius(0.88D);
		plot.addLayer(pin);
		JFreeChart jfreechart = new JFreeChart(plot);
		jfreechart.setTitle(channel.toString());
		ChartPanel chartpanel = new ChartPanel(jfreechart);
		chartpanel.setPreferredSize(new Dimension(400, 250));
		this.add(chartpanel, BorderLayout.CENTER);
		this.setPreferredSize(new Dimension(200,200));
		this.dataset.setValue(this.channel.getLastValue());
		return this;
	}
	
	@Override
	public void updateTile() {
		if(this.active){
			this.dataset.setValue(this.channel.getLastValue());
		}
	}
		
	public MeterTile clone() {
		return new MeterTile();
	}

	@Override
	public boolean isDigital() {
		return false;
	}

	@Override
	public boolean isInput() {
		return true;
	}

	@Override
	public boolean isOutput() {
		return false;
	}

	@Override
	public boolean isUpdateAble() {
		return true;
	}

	@Override
	public void setDimension(int width, int heigh) {
		this.setPreferredSize(new Dimension(width, heigh));		
	}

	@Override
	public void closeTile() {
		this.channel = null;
		
	}

	@Override
	protected void hubStart() {
		// TODO Auto-generated method stub
		
	}

	@Override
	protected void hubStop() {
		this.dataset.setValue(this.channel.getLastValue());		
	}
}
